#include <iostream>
using namespace std;
#pragma warning(disable : 4996)
#include "program.h"
#include "PowerExercise.h"
#include "AerobicExercise.h"

const char *ExeType[3] = {"All","Power","Aerobic"};
							

Program::Program() 
	: /*lastUpdateDate(),*/ allExercises(NULL), numOfExercises(0), phisicalSizeOfExercisesArr(2)
{
	lastUpdateDate.setToday();			//#WD[1] RonnyR 3.12
	allExercises = new Exercise*[phisicalSizeOfExercisesArr];

}

Program::Program(const Program& other) 
	: lastUpdateDate(other.lastUpdateDate), 
	  numOfExercises(other.numOfExercises), 
	  phisicalSizeOfExercisesArr(other.phisicalSizeOfExercisesArr)
{
	allExercises = new Exercise*[other.numOfExercises];
//	for (int i=0 ; i < numOfExercises ; i++)
//		allExercises[i] = new Exercise(*other.allExercises[i]);
}

Program::~Program()
{
	for (int i=0 ; i < numOfExercises ; i++)
		delete allExercises[i];
	delete []allExercises;
}

/*void Program::show() const
{
	if (numOfExercises == 0)
	{
		cout << "No Exercises yet!\n";
		return;
	}

	cout << "The exercises in the program:\n";
	for (int i=0 ; i < numOfExercises ; i++)
		cout<<*allExercises[i];//->show();  //#WD[2] RonnyR 3.12
	cout << "Program last update date: ";   //#WD[2] RonnyR 3.12
	cout<<lastUpdateDate;					//#WD[2] RonnyR 3.12
	cout << endl;
}
*/

void Program::addExercise(const Exercise& newExercise)
{
	if (numOfExercises == phisicalSizeOfExercisesArr)
	{
		phisicalSizeOfExercisesArr *= 2;
		Exercise** temp = new Exercise*[phisicalSizeOfExercisesArr];
		for (int i=0 ; i < numOfExercises ; i++)
			temp[i] = allExercises[i];

		delete []allExercises;
		allExercises = temp;
	}

	allExercises[numOfExercises] = newExercise.Clone(); //new Exercise(newExercise);
	numOfExercises++;
	lastUpdateDate.setToday();
}

void Program::removeExercise(const char* deviceName)
{
	for (int i=0 ; i < numOfExercises ; i++)
	{
		if (strcmp(deviceName, allExercises[i]->getDevice().getName()) == 0)
		{
			delete allExercises[i];
			for (int j=i+1 ; j < numOfExercises ; j++)
				allExercises[j-1] = allExercises[j];
			
			numOfExercises--;
			lastUpdateDate.setToday();
			break;
		}
	}
}

const Program & Program::operator=(const Program & P)				     //#WD[2] RonnyR 3.12
{
	if (this != &P)
	{
	
		for (int i=0 ; i < numOfExercises ; i++)
			delete allExercises[i];
		numOfExercises = 0;
		
		this->phisicalSizeOfExercisesArr = P.phisicalSizeOfExercisesArr;
		this->lastUpdateDate			 = P.lastUpdateDate;

	for (int i=0 ; i < P.numOfExercises ; i++)
		*this += *P.allExercises[i];
	}

return *this;
	
}
ostream & operator<<(ostream & os,const Program & P) //#WD[2] RonnyR 3.12
{

	if (P.numOfExercises == 0)
	{
		cout << "No Exercises yet!\n";
		return os;
	}

	cout << "The exercises in the program:\n";
	for (int i=0 ; i < P.numOfExercises ; i++)
		cout<<*(P.getAllExercises())[i];//->show();  //#WD[2] RonnyR 3.12
	cout << "Program last update date: ";   //#WD[2] RonnyR 3.12
	cout<<P.getLastUpdateDate();			//#WD[2] RonnyR 3.12
	cout << endl;

return os;	
}
const Program & Program::operator+=(const Exercise & E)
{
	addExercise(E);	
	return *this;
}
const Program & Program::operator-=(const char *name)
{
	removeExercise(name);
	return *this;	
}

void Program::PrintExercisesByType(int type) const
{

	if (numOfExercises == 0)
		cout << "No Exercises yet!\n";

	cout << "The ["<<ExeType[type]<<"] exercises in the program:\n";
	
	for (int i=0 ; i < numOfExercises ; i++)
		(*(getAllExercises())[i]).PrintExercise(cout,type);
	
	cout << "Program last update date: ";   //#WD[2] RonnyR 3.12
	cout << getLastUpdateDate();			//#WD[2] RonnyR 3.12
	cout << endl;




}